using System.Web.Mvc;
using Uber.Core.Service;
using System;
using Uber.WebUI.Models;

namespace Uber.WebUI.Controllers
{
    public class SuccessController : Controller
    {
        private readonly IAccounts accounts;

        public SuccessController(IAccounts accounts)
        {
            this.accounts = accounts;
        }

        public ActionResult Created(string email)
        {
            return View(new ActivateInput
                            {
                                Email = email,
                            });
        }

        public ActionResult ActivateAccount(string code, string email)
        {
            var gcode = Convert.ToInt64(code.Trim());
            if (accounts.Activate(gcode, email))
                return View("Activated");

            ModelState.AddModelError("Message", "The account was already activated or it doesn't exist");
            return View("Created", new ActivateInput
                                       {
                                           Email = email
                                       });
        }

        public ActionResult Activated()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Resend(string email)
        {
            var s = accounts.ResendActivationCode(email);
            ModelState.AddModelError("Message", s ?? "The email was resent, please check your mail");

            return View("Created", new ActivateInput
                                       {
                                           Email = email,
                                       });
        }
    }
}
